package com.bree.erp.jsf.datatable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.log4j.Logger;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.bree.erp.entity.MasterItem;
import com.bree.erp.service.MasterItemService;

public class LazyMasterItemDataModel extends LazyDataModel<MasterItem>{
	

	private static Logger LOG = Logger.getLogger(LazyMasterItemDataModel.class);

	private static final long serialVersionUID = 1L;
	
	private MasterItemService service;
	
	public LazyMasterItemDataModel(MasterItemService service) {
		this.service = service;
	}
	
	@Override
	public MasterItem getRowData(String rowKey) {
		
		long id = Long.parseLong(rowKey);
		
		return service.read(id);
	}
	

    @Override
    public Object getRowKey(MasterItem masterItem) {
    	return masterItem.getId();
    }

    @Override
    public List<MasterItem> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
    	
    	List<MasterItem> items = new ArrayList<MasterItem>();
    	

        PropertyUtilsBean utilsBean = new PropertyUtilsBean();
    	
    	for(MasterItem item : service.list()) {

            boolean match = true;
 
            if (filters != null) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    try {
                        String filterProperty = it.next();
                        Object filterValue = filters.get(filterProperty);
                        
                        String fieldValue = String.valueOf(utilsBean.getProperty(item, filterProperty));
 
                        if(filterValue == null || fieldValue.startsWith(filterValue.toString())) {
                            match = true;
                    }
                    else {
                            match = false;
                            break;
                        }
                    } catch(Exception e) {
                    	
                    	LOG.error(e.getMessage(), e);
                        match = false;
                    }
                }
            }
 
            if(match) {
                items.add(item);
            }
    	}
    	
        //sort
        if(sortField != null) {
            Collections.sort(items, new LazySorter<MasterItem>(sortField, sortOrder));
        }
 
        //rowCount
        int dataSize = items.size();
        this.setRowCount(dataSize);
 
        //paginate
        if(dataSize > pageSize) {
            try {
                return items.subList(first, first + pageSize);
            }
            catch(IndexOutOfBoundsException e) {
                return items.subList(first, first + (dataSize % pageSize));
            }
        }
        else {
            return items;
        }
    }
}
